﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace HRdex_Final.Forms
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        string SessionID()
        {
            
            return Session["EmployeeID"].ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SqlCommand cmd22 = new SqlCommand("SELECT EmployeeID, LastName + ', ' + FirstName + ' ' + MiddleName AS Name, DeptID, catPositionsID, CollegeID FROM formEmployees WHERE (EmployeeID =" + SessionID() + ")", conn);
            SqlDataAdapter da22 = new SqlDataAdapter(cmd22);
            DataTable dt22 = new DataTable("dt22");
            try
            {
                da22.Fill(dt22);

                txtEmpNo.Text = dt22.Rows[0].ItemArray[0].ToString();
                txtEmpName.Text = dt22.Rows[0].ItemArray[1].ToString();
                ddlDepartment.SelectedValue= dt22.Rows[0].ItemArray[2].ToString();
                ddlPositions.SelectedValue = dt22.Rows[0].ItemArray[3].ToString();
                ddlOffice.SelectedValue = dt22.Rows[0].ItemArray[4].ToString();
            }
            catch (Exception)
            {
                
                
            }



            txtOTime.Visible = false;
            ddlPositions.Enabled = false;
            ddlOffice.Enabled = false;
            txtEmpName.Enabled = false;
            txtEmpNo.Enabled = false;
            lblConfirmationMessage.Visible = false;
            ddlApprovalHead.Visible = false;
            ddlApprovalDean.Visible = false;
            ddlApprovalHR.Visible = false;
            txtfTime.Visible = false;
            lblChair.Visible = false;
            lblDean.Visible = false;
            lblDirector.Visible = false;
            txtAppHRflexi.Visible = false;
            txtAppDeanflexi.Visible = false;
            txtAppChairflexi.Visible = false;
            ddlDepartment.Enabled = false;

            
            Selected();
            //autoTime();
           
        }   
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            string FlexiID = Request.QueryString["FlexiID"];
            if (txtFlexiID.Text != FlexiID)
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                DataSet ds = new DataSet();

                conn.Open();

                SqlCommand cmd = new SqlCommand("Insert Into formFlexi (EmpID, CollegeID, PositionID, " +
                    "originalDate, originalTime, ActivityDesc, FlexiDate, FlexiTime, PubDate, DeptID)" +
                    "Values (@empID, @collegeID, @posID, @oDate, @oTime, @actDesc, @fDate," +
                    "@fTime, @pubDate, @dept)", conn);

                cmd.Parameters.Add("@empID", SqlDbType.Int).Value = txtEmpNo.Text;
                cmd.Parameters.Add("@collegeID", SqlDbType.Int).Value = ddlOffice.SelectedValue;
                cmd.Parameters.Add("@posID", SqlDbType.Int).Value = ddlPositions.SelectedValue;
                cmd.Parameters.Add("@oDate", SqlDbType.VarChar).Value = txtODate.Text;
                cmd.Parameters.Add("@oTime", SqlDbType.VarChar).Value = ddlOhr.Text + ":" + ddlOmin.Text + "-" + ddlOhr0.Text + ":" + ddlOmin.Text;
                cmd.Parameters.Add("@actDesc", SqlDbType.VarChar).Value = txtActivityReason.Text;
                cmd.Parameters.Add("@fDate", SqlDbType.VarChar).Value = txtfDate.Text;
                cmd.Parameters.Add("@fTime", SqlDbType.VarChar).Value = ddlFlexiHour.Text + ":" + ddlFlexiMin.Text + "-" + ddlFlexihr0.Text + ":" + ddlFleximin1.Text;
                cmd.Parameters.Add("@pubDate", SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("@dept", SqlDbType.Int).Value = ddlDepartment.SelectedValue;
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    
                    
                }


                conn.Close();
                Panel1.Visible = false;
                lblConfirmationMessage.Visible = true;
                lblConfirmationMessage.Text = "You successfully request a flexi time. Please wait for the confirmation upon reviewing your schedule.";
            }
            else
            {
                Edit();
                btnSubmit.Visible = false;
            }
        }
        public void Selected()
        {
            string FlexiID = Request.QueryString["FlexiID"];
            if (FlexiID != null)
            {

                btnClear.Visible = false;
                txtfDate.Visible = true;
                txtODate.Visible = true;
                txtfDate.Enabled = false;
                txtODate.Enabled = false;
                ddlOhr.Visible = false;
                ddlOhr0.Visible = false;
                ddlOmin.Visible = false;
                ddlOmin0.Visible = false;
                ddlFlexiHour.Visible = false;
                ddlFlexihr0.Visible = false;
                ddlFlexiMin.Visible = false;
                ddlFleximin1.Visible = false;
                btnSubmit.Visible = false;
                txtOTime.Visible = true;
                txtfTime.Visible = true;
                txtOTime.Enabled = false;
                txtfTime.Enabled = false;
                lblDash.Visible = false;
                lblDash0.Visible = false;
                txtActivityReason.Enabled = false;
                ddlDepartment.Enabled = false;
                
                //Button
                btnClear.Visible = false;

                //approval disable
                ddlDepartment.Enabled = false;
                ddlApprovalDean.Visible = false;
                ddlApprovalHead.Visible = false;
                ddlApprovalHR.Visible = false;

                //textbox output enable
                txtAppChairflexi.Visible = true;
                txtAppDeanflexi.Visible = true;
                txtAppHRflexi.Visible = true;
                txtAppChairflexi.Enabled = false;
                txtAppDeanflexi.Enabled = false;
                txtAppHRflexi.Enabled = false;
                lblChair.Visible = true;
                lblDean.Visible = true;
                lblDirector.Visible = true;

                SqlCommand cmd2 = new SqlCommand("SELECT formFlexi.FlexiID, formEmployees.LastName + ', ' + formEmployees.FirstName + ' ' + formEmployees.MiddleName AS Name, " +
                    "formEmployees.EmployeeID, formFlexi.PositionID, formFlexi.CollegeID, " +
                    "formFlexi.FlexiDate, formFlexi.FlexiTime, formFlexi.ActivityDesc, formFlexi.OriginalDate, " +
                    "formFlexi.OriginalTime, formFlexi.pubDate, formFlexi.HeadApproval, formFlexi.DeanApproval, formFlexi.HRApproval FROM formFlexi INNER JOIN formEmployees ON " +
                    "formFlexi.EmpID = formEmployees.EmployeeID AND formFlexi.EmpID = formEmployees.EmployeeID " +
                    "WHERE (FlexiID = " + FlexiID + ")", conn);
                SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
                DataTable dt2 = new DataTable("dt2");

                try
                {
                    da2.Fill(dt2);
                    txtFlexiID.Text = dt2.Rows[0].ItemArray[0].ToString();
                    txtEmpNo.Text = dt2.Rows[0].ItemArray[2].ToString();
                    txtEmpName.Text = dt2.Rows[0].ItemArray[1].ToString();
                    ddlPositions.SelectedValue = dt2.Rows[0].ItemArray[3].ToString();
                    ddlOffice.SelectedValue = dt2.Rows[0].ItemArray[4].ToString();
                    txtfDate.Text = dt2.Rows[0].ItemArray[5].ToString();
                    txtfTime.Text = dt2.Rows[0].ItemArray[6].ToString();
                    txtActivityReason.Text = dt2.Rows[0].ItemArray[7].ToString();
                    txtODate.Text = dt2.Rows[0].ItemArray[8].ToString();
                    txtOTime.Text = dt2.Rows[0].ItemArray[9].ToString();
                    lblDatePub.Text = dt2.Rows[0].ItemArray[10].ToString();
                    txtAppChairflexi.Text = dt2.Rows[0].ItemArray[11].ToString();
                    txtAppDeanflexi.Text = dt2.Rows[0].ItemArray[12].ToString();
                    txtAppHRflexi.Text = dt2.Rows[0].ItemArray[13].ToString();
                }
                catch (Exception)
                {
                    
                    
                }


                #region ApprovedDenied

                SqlCommand cmd31 = new SqlCommand("SELECT formEmployees.EmployeeID, catUserLevel.catUserLevelName AS [User Level], catPositions.catPositionsName AS Position, catDepartment.DepartmentName AS Department, catCollege.CollegeName AS Office FROM formEmployees INNER JOIN catDepartment ON formEmployees.DeptID = catDepartment.DeptID INNER JOIN catCollege ON formEmployees.CollegeID = catCollege.CollegeID INNER JOIN catPositions ON formEmployees.catPositionsID = catPositions.catPositionsID INNER JOIN catUserLevel ON formEmployees.UserLevelID = catUserLevel.UserLevelID WHERE (EmployeeID = '" + SessionID() + "')", conn);
                SqlDataAdapter da31 = new SqlDataAdapter(cmd31);
                DataTable dt31 = new DataTable("dt31");
                try
                {
                    da31.Fill(dt31);


                    if (dt31.Rows[0].ItemArray[1].ToString() == "Admin" || dt31.Rows[0].ItemArray[1].ToString() == "SubAdmin")
                    {
                        if (dt31.Rows[0].ItemArray[4].ToString() == "Human Resource Management and Development" && dt31.Rows[0].ItemArray[3].ToString() == "HR Directors Office" && dt31.Rows[0].ItemArray[1].ToString() == "Admin" && dt31.Rows[0].ItemArray[2].ToString() == "Director" || dt31.Rows[0].ItemArray[4].ToString() == "Human Resource Management and Development" && dt31.Rows[0].ItemArray[3].ToString() == "HR Salaries and Benefits" && dt31.Rows[0].ItemArray[1].ToString() == "SubAdmin" && dt31.Rows[0].ItemArray[2].ToString() == "Head")
                        {
                            btnSubmit.Visible = true;
                            ddlApprovalHead.Visible = false;
                            ddlApprovalDean.Visible = false;
                            ddlApprovalHR.Visible = true;
                            txtAppChairflexi.Visible = true;
                            txtAppDeanflexi.Visible = true;
                            txtAppHRflexi.Visible = false;
                            if (dt2.Rows[0].ItemArray[12].ToString() == "Approved" && dt2.Rows[0].ItemArray[13].ToString() != "Denied" && dt2.Rows[0].ItemArray[13].ToString() != "Approved")
                            {
                                ddlApprovalDean.Enabled = false;
                                ddlApprovalHead.Enabled = false;
                                ddlApprovalHR.Enabled = true;
                            }
                            else
                            {
                                txtAppChairflexi.Visible = true;
                                txtAppDeanflexi.Visible = true;
                                txtAppHRflexi.Visible = true;
                                ddlApprovalDean.Visible = false;
                                ddlApprovalHead.Visible = false;
                                ddlApprovalHR.Visible = false;
                            }
                        }
                    }
                    //Head Approval

                    else if (dt31.Rows[0].ItemArray[1].ToString() == "Faculty" || dt31.Rows[0].ItemArray[1].ToString() == "Employee")
                    {
                        if (dt31.Rows[0].ItemArray[2].ToString() == "Chairperson" || dt31.Rows[0].ItemArray[2].ToString() == "Head" || dt31.Rows[0].ItemArray[2].ToString() == "Supervisor")
                        {
                            txtAppChairflexi.Visible = false;
                            txtAppDeanflexi.Visible = false;
                            txtAppHRflexi.Visible = false;
                            btnSubmit.Visible = true;
                            ddlApprovalHead.Visible = true;
                            ddlApprovalDean.Visible = true;
                            ddlApprovalHR.Visible = true;


                            if (dt2.Rows[0].ItemArray[11].ToString() != "Denied" && dt2.Rows[0].ItemArray[11].ToString() != "Approved")
                            {
                                ddlApprovalDean.Enabled = false;
                                ddlApprovalHead.Enabled = true;
                                ddlApprovalHR.Enabled = false;
                            }
                            else
                            {
                                txtAppChairflexi.Visible = true;
                                txtAppDeanflexi.Visible = true;
                                txtAppHRflexi.Visible = true;
                                ddlApprovalDean.Visible = false;
                                ddlApprovalHead.Visible = false;
                                ddlApprovalHR.Visible = false;
                            }
                        }

                        else if (dt31.Rows[0].ItemArray[2].ToString() == "Dean" || dt31.Rows[0].ItemArray[2].ToString() == "Director")
                        {
                            btnSubmit.Visible = true;
                            ddlApprovalHead.Visible = false;
                            ddlApprovalDean.Visible = true;
                            ddlApprovalHR.Visible = true;
                            txtAppChairflexi.Visible = true;
                            txtAppDeanflexi.Visible = false;
                            txtAppHRflexi.Visible = false;
                            if (dt2.Rows[0].ItemArray[11].ToString() == "Approved" && dt2.Rows[0].ItemArray[12].ToString() != "Denied" && dt2.Rows[0].ItemArray[12].ToString() != "Approved")
                            {

                                ddlApprovalDean.Enabled = true;
                                ddlApprovalHead.Enabled = false;
                                ddlApprovalHR.Enabled = false;
                            }
                            else
                            {
                                txtAppChairflexi.Visible = true;
                                txtAppDeanflexi.Visible = true;
                                txtAppHRflexi.Visible = true;
                                ddlApprovalDean.Visible = false;
                                ddlApprovalHead.Visible = false;
                                ddlApprovalHR.Visible = false;
                            }
                        }
                    }
                    else
                    {
                        ddlApprovalHead.Visible = true;
                        ddlApprovalDean.Visible = true;
                        ddlApprovalHR.Visible = true;
                        txtAppChairflexi.Visible = false;
                        txtAppDeanflexi.Visible = false;
                        txtAppHRflexi.Visible = false;
                    }
                }
                catch (Exception)
                {
                    
                   
                }

                #endregion
            }
        }

        public void Edit()
        {
            string FlexiID = Request.QueryString["FlexiID"];
            //System Head Approved
            if (ddlApprovalHead.Text == "Approved" || ddlApprovalHead.Text == "Denied")
            {
                SqlCommand cmd = new SqlCommand("UPDATE formFlexi SET  HeadApproval ='" + ddlApprovalHead.Text + "' WHERE (FlexiID = " + FlexiID + ")", conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                conn.Open();
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    
                    throw;
                }

                conn.Close();
                lblConfirmationMessage.Visible = true;
                lblConfirmationMessage.Text = "You successfully give your approval status..";
                ddlApprovalDean.Enabled = false;
                //Notifiction
                notification(ddlApprovalHead.Text, 0);
            }
            //System Dean Approval

            if (ddlApprovalDean.Text == "Approved" || ddlApprovalDean.Text == "Denied")
            {
                SqlCommand cmd22 = new SqlCommand("UPDATE formFlexi SET  DeanApproval ='" + ddlApprovalDean.Text + "' WHERE (FlexiID = " + FlexiID + ")", conn);
                SqlDataAdapter da22 = new SqlDataAdapter(cmd22);
                conn.Open();
                cmd22.ExecuteNonQuery();
                conn.Close();
                lblConfirmationMessage.Visible = true;
                lblConfirmationMessage.Text = "You successfully give your approval status..";
                ddlApprovalDean.Enabled = false;
                //Notification
                notification(ddlApprovalDean.Text, 2);
               
            }
            //System Dean Approval

            else if (ddlApprovalHR.Text == "Approved" || ddlApprovalHR.Text == "Denied")
            {
                SqlCommand cmd33 = new SqlCommand("UPDATE formFlexi SET  HRApproval ='" + ddlApprovalHR.Text + "' WHERE (FlexiID = " + FlexiID + ")", conn);
                SqlDataAdapter da33 = new SqlDataAdapter(cmd33);
                conn.Open();
                cmd33.ExecuteNonQuery();
                conn.Close();
                lblConfirmationMessage.Visible = true;
                lblConfirmationMessage.Text = "You successfully give your approval status..";
                ddlApprovalHR.Enabled = false;
                //Notification
                notification(ddlApprovalHR.Text, 1);
            }

        }
        void notification(string result, int userlvl)
        {
            string FlexiID = Request.QueryString["FlexiID"];
            string desc;
   
            switch (userlvl)
            {
                case 1:
                    desc = "Your Application has been " + result + " by the HR Director";
                    break;
                case 2:
                    desc = "Your Application has been " + result + " Dean/Director";
                    break;
                default:
                    desc = "Your Application has been " + result + " by the Dept. Head/Supervisor";
                    break;
            }


            string sqlc = "INSERT INTO formMessegeSender " +
                "(Subject, Description, DateTime, EmployeeID, catMessegeID, fromEmpID, Readed) " +
                "VALUES ('Flexiform Request','" +
                desc + "','" +
                DateTime.Now + "'," +
                cbasender(FlexiID) +
                ",2," +
                SessionID() +
                ", 0)";
            SqlCommand cmd = new SqlCommand(sqlc, conn);

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        string cbasender(string src)
        {
            string sqlx = "SELECT EmpID FROM formFlexi WHERE FlexiID = " + src;
            SqlDataAdapter cmd = new SqlDataAdapter(sqlx, conn);
            DataTable tbl = new DataTable();

            conn.Open();
            cmd.Fill(tbl);
            conn.Close();

            return tbl.Rows[0][0].ToString();
        }


        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtActivityReason.Text = null;
            txtEmpName.Text = null;
            txtEmpNo.Text = null;
            txtfDate.Text = null;
            txtfTime.Text = null;
            txtODate.Text = null;
            txtOTime.Text = null;
            //txtfTotal.Text = null;
            txtOTime.Text = null;
        }

    }
}